package com.mldong.modules.sys.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.mldong.auth.Perm;
import com.mldong.base.CommonPage;
import com.mldong.base.CommonResult;
import com.mldong.base.IdParam;
import com.mldong.base.IdsParam;
import com.mldong.holder.LoginUserHolder;
import com.mldong.modules.sys.dto.MenuPageParam;
import com.mldong.modules.sys.dto.MenuParam;
import com.mldong.modules.sys.dto.SyncRouteParam;
import com.mldong.modules.sys.entity.Dept;
import com.mldong.modules.sys.entity.Menu;
import com.mldong.modules.sys.service.MenuService;
import com.mldong.modules.sys.vo.MenuVO;
import com.mldong.modules.sys.vo.RouteMenuVO;
import com.mldong.validation.Groups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
/**
* <p>
    * 菜单 前端控制器
    * </p>
*
* @author mldong
* @since 2022-10-20
*/
@RestController
@Api(tags = "菜单管理")
@RequiredArgsConstructor
public class MenuController {
    private final MenuService menuService;
    /**
     * 添加菜单
     * @param param
     * @return
     */
    @PostMapping("/sys/menu/save")
    @ApiOperation(value = "添加菜单")
    @Perm
    public CommonResult<?> save(@RequestBody @Validated({Groups.Save.class}) MenuParam param) {
        menuService.save(param);
        return CommonResult.ok();
    }
    /**
     * 删除菜单
     * @param param
     * @return
     */
    @PostMapping("/sys/menu/remove")
    @ApiOperation(value = "删除菜单")
    @Perm
    public CommonResult<?> remove(@RequestBody IdsParam param) {
        menuService.removeBatchByIds(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 修改菜单
     * @param param
     * @return
     */
    @PostMapping("/sys/menu/update")
    @ApiOperation(value = "修改菜单")
    @Perm
    public CommonResult<?> update(@RequestBody @Validated({Groups.Update.class}) MenuParam param) {
        menuService.update(param);
        return CommonResult.ok();
    }
    /**
     * 查询单个菜单
     * @param param
     * @return
     */
    @PostMapping("/sys/menu/detail")
    @ApiOperation(value = "查询单个菜单")
    @Perm
    public CommonResult<MenuVO> getById(@RequestBody IdParam param) {
        MenuVO menu = menuService.findById(param.getId());
        return CommonResult.data(menu);
    }
    /**
     *分页查询菜单列表
     * @param param
     * @return
     */
    @PostMapping("/sys/menu/page")
    @ApiOperation(value = "分页查询菜单列表")
    @Perm
    public CommonResult<CommonPage<MenuVO>> page(@RequestBody MenuPageParam param) {
        return CommonResult.data(menuService.page(param));
    }
    /**
     *查询菜单列表
     * @param param
     * @return
     */
    @PostMapping("/sys/menu/list")
    @ApiOperation(value = "查询菜单列表")
    @Perm
    public CommonResult<List<Menu>> list(@RequestBody MenuPageParam param) {
        if(StrUtil.isBlank(param.getOrderBy())){
            param.setOrderBy("sort asc");
        }
        return CommonResult.data(menuService.list(param.buildQueryWrapper()));
    }
    @PostMapping("/sys/menu/syncRoute")
    @ApiOperation(value = "同步前端路由")
    @Perm
    public CommonResult<?> syncRoute(@RequestBody List<SyncRouteParam> params,@RequestHeader(required = false,defaultValue = "platform") String appCode) {
        menuService.syncRoute(appCode,params);
        return CommonResult.ok();
    }
    /**
     *查询菜单树
     * @param param
     * @return
     */
    @PostMapping(value = {"/sys/menu/tree","/sys/menu/userTree"})
    @ApiOperation(value = "查询菜单树")
    @Perm(value = {"sys:rbac:saveRoleMenu","sys:menu:tree","sys:menu:userTree"},mode = Perm.OR)
    public CommonResult<List<MenuVO>> tree(@RequestBody MenuPageParam param) {
        return CommonResult.data(menuService.tree(param));
    }
    /**
     *获取用户菜单
     * @return
     */
    @GetMapping(value = "/menu/all")
    @ApiOperation(value = "获取用户菜单")
    public CommonResult<List<RouteMenuVO>> menuAll() {
        return CommonResult.data(menuService.userRouteMenu(LoginUserHolder.getUserId()));
    }
}
